﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace MO.ModelingClient
{
    public partial class ItemFieldControl : ItemBaseControl
    {
        public ItemFieldControl(string title)
        {
            InitializeComponent();
            base.Init(title);
            comboBoxEntity.ValueMember = "ED_Code";
            comboBoxEntity.DisplayMember = "ED_Name";
            comboBoxField.ValueMember = "FD_Code";
            comboBoxField.DisplayMember = "FD_Name";
            var entitys = DbOperation.DB.ExecuteDataTable("select ED_Code,ED_Name from Entity_Define");  //DataManage.FindAllEntity();
            entitys.Rows.Add(entitys.NewRow());
            comboBoxEntity.DataSource = entitys;
        }

        private void comboBoxEntity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxEntity.SelectedIndex < 0)
                return;
            if (string.IsNullOrEmpty(comboBoxEntity.SelectedValue as string))
            {
                ((DataTable)comboBoxField.DataSource).Rows.Clear();
                return;
            }
            comboBoxField.DataSource = DbOperation.DB.ExecuteDataTable("select FD_Code,FD_Name from Field_Define  where ED_Code={0}", comboBoxEntity.SelectedValue);
        }
        public override void SetValue(object value)
        {
            string code = value as string;
            if (string.IsNullOrEmpty(code))
                return;
            var field = DataManager.FindFieldByFieldCode(code);
            if (field == null)
                return;
            comboBoxEntity.SelectedValue = field.EntityCode;
            comboBoxField.SelectedValue = code;
        }
        public override object GetValue()
        {
            return comboBoxField.SelectedValue;
        }
        public override void SetEnabled(bool enabled)
        {
            this.comboBoxEntity.Enabled = enabled;
            this.comboBoxField.Enabled = enabled;
        }
    }
}
